#include "my_math.h"
#include "complement_line.h"

void Complement_line(ImageInfo &image_info)
{
	if (image_info.Element.Cross)
		Complement_Cross(image_info);
	// 若无法判断元素，则判断是否出现20-100行出现大量的宽度突变
	else
		Lose_toplines(image_info);
}

void Complement_Cross(ImageInfo &image_info)
{
	// 1.正入十字
	if (image_info.Element.Cross == 1)
	{
		int Cross_X = 0;
		int Cross_Y = 0;
		// TODO 此处可为参数
		// TODO 此处只判断右边线
		for (int Ysite = 90; Ysite > image_info.ImageStatus.OFFLine + 5; Ysite--)
			if (image_info.ImageDeal[Ysite - 1].RightBorder - image_info.ImageDeal[Ysite].RightBorder < -15)
			{
				Cross_X = image_info.ImageDeal[Ysite - 2].RightBorder;
				Cross_Y = Ysite - 2;
				break;
			}
		// TODO 此处可为参数
		int Calibration_Y = RESULT_WIDTH - 2;
		if (Cross_X)
		{
			float Slope = Calculate_Slope(Cross_X, Cross_Y, image_info.ImageDeal[Calibration_Y].RightBorder, Calibration_Y);
			for (int Ysite = Cross_Y; Ysite < Calibration_Y; Ysite++)
				image_info.ImageDeal[Ysite].RightBorder = image_info.ImageDeal[Cross_Y].RightBorder + (Ysite - Cross_Y) * Slope;
			for (int Ysite = image_info.ImageStatus.OFFLine; Ysite < Calibration_Y; Ysite++)
				image_info.ImageDeal[Ysite].Center = image_info.ImageDeal[Ysite].RightBorder - Half_Wide;
		}
	}
}

void Lose_toplines(ImageInfo &image_info)
{
	/*
	逆透视后在转弯后在0-20行有大量的丢行同时无法判断元素
	在十字回环里面出环十字丢线无法知道是什么元素
	正入直到前方刚刚丢线
	我们对边边线进行整合处理
	处理方法，做底部直线的延长线
	*/
	int toplines_num = 0;  // 顶部宽度超过限度的数量
	int downlines_num = 0; // 底部宽度超过限度的数量
	char Direction = 0;  // 'L'：右边大量丢线延长右边线, 'R'：左边大量丢线延长左边线
	/*前瞻小于一定的值*/
	if (image_info.ImageStatus.OFFLine > 5)
		return;

	/*判断一：判断左右方向是否存在大量丢线情况*/
	if (image_info.ImageStatus.whitelines_L + image_info.ImageStatus.whitelines_R > 40)
	{
		if (image_info.ImageStatus.whitelines_L >= image_info.ImageStatus.whitelines_R)
			Direction = 'R';
		else if (image_info.ImageStatus.whitelines_L < image_info.ImageStatus.whitelines_R)
			Direction = 'L';
	}
	/*判断二:顶部是否存在单边增长*/
	if (Direction)
	{
		if (Direction == 'R')
			for (int Ysite = image_info.ImageStatus.OFFLine; Ysite < image_info.ImageStatus.OFFLine + 30; Ysite++)
				if (image_info.ImageDeal[Ysite].RightBorder > image_info.ImageDeal[Ysite + 10].RightBorder ||
					image_info.ImageDeal[Ysite].RightBorder > RESULT_WIDTH - 5)
					toplines_num++;
		if (Direction == 'L')
			for (int Ysite = image_info.ImageStatus.OFFLine; Ysite < image_info.ImageStatus.OFFLine + 30; Ysite++)
				if (image_info.ImageDeal[Ysite].LeftBorder < image_info.ImageDeal[Ysite + 10].RightBorder ||
					image_info.ImageDeal[Ysite].RightBorder < 5)
					toplines_num++;
	}
	if (toplines_num < 20)
		return;

	/*判断三：判断左右底部是正常宽度*/
	for (int Ysite = RESULT_WIDTH - 2; Ysite > 70; Ysite--)
		if (image_info.ImageDeal[Ysite].Wide < 60)
			downlines_num++;
	if (downlines_num < 20)
		return;

	/*判断四：计算斜率进行补线*/
	if (Direction == 'R')
	{
		int Calibration_Y = 70;
		float Slope = Calculate_Slope(image_info.ImageDeal[Calibration_Y].RightBorder, Calibration_Y, image_info.ImageDeal[85].RightBorder, 85);

		for (int Ysite = Calibration_Y + 10; Ysite > image_info.ImageStatus.OFFLine; Ysite--)
		{
			image_info.ImageDeal[Ysite].RightBorder = image_info.ImageDeal[Calibration_Y].RightBorder + (Ysite - Calibration_Y) * Slope;
			image_info.ImageDeal[Ysite].Center = image_info.ImageDeal[Ysite].RightBorder - Half_Wide;
			if (image_info.ImageDeal[Ysite].Center < 0)
			{
				image_info.ImageStatus.OFFLine = Ysite + 2;
				break;
			}
		}
	}
	else if (Direction == 'L')
	{
		int Calibration_Y = 70;
		float Slope = Calculate_Slope(image_info.ImageDeal[Calibration_Y].LeftBorder, Calibration_Y, image_info.ImageDeal[85].LeftBorder, 85);

		for (int Ysite = Calibration_Y + 10; Ysite > image_info.ImageStatus.OFFLine; Ysite--)
		{
			image_info.ImageDeal[Ysite].LeftBorder = image_info.ImageDeal[Calibration_Y].LeftBorder + (Ysite - Calibration_Y) * Slope;
			image_info.ImageDeal[Ysite].Center = image_info.ImageDeal[Ysite].LeftBorder + Half_Wide;
			if (image_info.ImageDeal[Ysite].Center > RESULT_WIDTH)
			{
				image_info.ImageStatus.OFFLine = Ysite + 2;
				break;
			}
		}
	}
}
